import { defineComponent, reactive, render, h, createVNode } from 'vue';
import Loading from './components/loading.vue';
export const createLoading = (props: any, target: any) => {
    const state = reactive({
        tip: '',
        loading: true,
        percent: 0,
        ...props
    });
    const loadingWrap = defineComponent({
        render() {
            return h(Loading, { ...state });
        }
    });
    const vdom = createVNode(loadingWrap);
    let container: any = undefined;
    container = (document as any).createElement('div');
    container && render(vdom, container);
    // 打开
    const open = () => {
        if (target.container) {
            if (target.container.parentNode === target) {
                return;
            }
        }
        target.appendChild(container);
        target.vm = vdom;
        target.container = container;
    };
    // 关闭
    const close = () => {
        if (target.container.parentNode) {
            target.container.parentNode.removeChild(target.container);
        }
    };
    open();
    const setTip = (tip: any) => {
        state.tip = tip;
    };
    const setPercent = (percent: any) => {
        state.percent = percent;
    };
    const setLoading = (loading: boolean) => {
        state.loading = loading;
    };
    return {
        loadingWrap,
        setTip,
        setPercent,
        open,
        close,
        setLoading
    };
};